package com.mofei.mall04_fileupload.common.util;

import net.coobird.thumbnailator.Thumbnails;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;

public class ImgUtil {
    /**
     * @Author Ragty
     * @Description 调整图片角度并生成合适大小的图片
     * @Date 16:37 2019/3/7
     * @return
     **/
    public BufferedImage rotateImage(String imgFile) throws Exception{
        //创建空的BufferedImage对象
        BufferedImage bufferedImage = null;
        try {
            if(imgFile != "" || imgFile != null){
                //若imgFile不为空，创建名为_img_file_的File对象
                File _img_file_ = new File(imgFile);
                //判断_img_file_该路径下的图片文件是否存在，若存在 执行相关操作
                if(_img_file_.exists()){
                    Integer angel = 270;
                    //对图片问价内进行读取（格式应为GIF, PNG, or JPEG）
                    BufferedImage src = ImageIO.read(_img_file_);
                    //获取图片的长宽，计算长宽比
                    BigDecimal height = BigDecimal.valueOf(src.getHeight());
                    BigDecimal width = BigDecimal.valueOf(src.getWidth());
                    BigDecimal bd = height.divide(width,4,BigDecimal.ROUND_CEILING);
                    //设置标准长宽比
                    BigDecimal theValue = BigDecimal.valueOf(1.1700);
                    //判断图片长宽比是否小于标准长宽比，若是 进行转换
                    if (bd.compareTo(theValue) == -1 ) {
                        System.out.println("转换中 ...");
                        //将图片旋转270°，不进行缩放，执行calcScale方法
                        bufferedImage = Thumbnails.of(src).rotate(angel).scale(1).asBufferedImage();
                        bufferedImage = calcScale(bufferedImage);
                    }else {
                        bufferedImage = calcScale(src);
                        System.out.println("宽高比合适,跳过 ...");
                    }

                    //bufferedImage = getSharperPicture(bufferedImage);

                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return bufferedImage;
    }


    /**
     * @Author Ragty
     * @Description  计算最佳放缩比
     * @Date 9:19 2019/3/11
     * @return
     **/
    public static BufferedImage calcScale(BufferedImage bufferedImage) throws IOException{
        //设置标准宽高，获取图片宽高
        BigDecimal standaraHeight = BigDecimal.valueOf(842);
        BigDecimal standaraWeight = BigDecimal.valueOf(595);
        BigDecimal height = BigDecimal.valueOf(bufferedImage.getHeight());
        BigDecimal width = BigDecimal.valueOf(bufferedImage.getWidth());
        //当图片宽/高大于标准宽度，时，进行缩放
        if (height.compareTo(standaraHeight) == 1 || width.compareTo(standaraWeight) == 1) { //不符合标准，缩放
            //获取图片的宽和高于标准的宽和高的比例
            BigDecimal scaleHeight = standaraHeight.divide(height,4,BigDecimal.ROUND_CEILING);
            BigDecimal scaleWidth = standaraWeight.divide(width,4,BigDecimal.ROUND_CEILING);
            //如果标准高：原图高的比例小于1，缩小bufferedImage
            if (scaleHeight.compareTo(BigDecimal.valueOf(1)) == -1) {
                bufferedImage = Thumbnails.of(bufferedImage).scale(scaleHeight.floatValue()).outputQuality(1.0f).asBufferedImage();
                calcScale(bufferedImage);
                //如果标准宽：原图宽的比例小于1，缩小bufferedImage
            }else if (scaleWidth.compareTo(BigDecimal.valueOf(1)) == -1) {
                bufferedImage = Thumbnails.of(bufferedImage).scale(scaleWidth.floatValue()).outputQuality(1.0f).asBufferedImage();
                calcScale(bufferedImage);
            }
        }

        return  bufferedImage;
    }

}
